﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Invoicing.Model.Products;
namespace Invoicing.Command.Products
{
    public class ProductCommand
    {
        public static ProductModel CreateNew(ProductModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var dProduct = Convertor.ConvertFrom<Product, ProductModel>(model);
                    var catids = model.Categories.Select(p => p.ID).ToArray();
                    var categories = db.DataContext.ProductCategories.Where(p => catids.Contains(p.ID)).ToList();
                    dProduct.ProductCategories = categories;
                    
                    db.DataContext.Products.Add(dProduct);
                    db.DataContext.SaveChanges();
                    model.ID = dProduct.ID;
                }
            }
            return null;
        }

        public static void Update(ProductModel model)
        {
            if (model != null && model.ID>0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dProduct = db.DataContext.Products.FirstOrDefault(p => p.ID == model.ID);
                    if (dProduct != null)
                    {
                        SaveCheck(model, db);
                        Convertor.UpdateValue(model, dProduct);
                        dProduct.ProductCategories.Clear();
                        var catids = model.Categories.Select(p => p.ID).ToArray();
                        var categories = db.DataContext.ProductCategories.Where(p => catids.Contains(p.ID)).ToList();
                        dProduct.ProductCategories = categories;
                        db.DataContext.SaveChanges();
                    }
                }
            }
        }

        private static void SaveCheck(ProductModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.Module))
                throw new ApplicationException("请提供产品型号");
        }

        public static void Delete(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<Product>(p => p.ID == id);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static ProductModel GetModel(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dProduct = db.DataContext.Products.FirstOrDefault(p => p.ID == id);
                    if (dProduct != null)
                    {
                        var model = Convertor.ConvertTo<ProductModel, Product>(dProduct);
                        if (dProduct.ProductCategories!= null && dProduct.ProductCategories.Count() > 0)
                            model.Categories =Convertor.ConvertCollection<ProductCategoryModel, ProductCategory,ProductCategoryCollection>(dProduct.ProductCategories);
                        return model;
                    }
                }
            }
            return null;
        }

        public static ProductCollection GetProducts(ProductQuery sea)
        {
            var result = new ProductCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Product> q = db.DataContext.Products;
                if (sea != null)
                {
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.Module.Contains(sea.Key) || p.ProductNo.Contains(sea.Key));
                    if (!string.IsNullOrEmpty(sea.Categories))
                    {
                        var cats = sea.Categories.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        if (cats.Length > 0)
                        {
                            var ids = cats.Select(p => int.Parse(p)).ToArray();
                            q = q.Where(p => p.ProductCategories.Any(o => ids.Contains(o.ID)));
                        }
                    }
                    if (sea.Status > 0)
                        q = q.Where(p => p.Status == sea.Status);
                    if (sea.PType > 0)
                        q = q.Where(p => p.PType == sea.PType);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.SortIndex);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<ProductModel, Product, ProductCollection>(lst);
                    result.TotalRecordsCount = n;
                    if (sea != null)
                    {
                        foreach (var pro in result)
                        {
                            var d = lst.Find(p => p.ID == pro.ID);
                            if (sea.GetCategories)
                            {
                                var cats = d.ProductCategories.ToList();
                                pro.Categories = Convertor.ConvertCollection<ProductCategoryModel, ProductCategory, ProductCategoryCollection>(cats);
                            }
                        }
                    }
                }
            }
            return result;
        }
    }
}
